﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Models;
using BLL;

namespace CSMF.Management.Page_LinkInfo
{
    public partial class Link_Add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["admin"] != null)
                {
                    BindDDLType();
                }
                else
                {
                    Response.Write("<script language='javascript'>parent.location.href='../login.aspx';</script>");
                }
            }
        }

        private void BindDDLType()
        {
            IList<LinkType> types = LinkTypeManager.GetAllLinkTypes();
            ddlLinkType.Items.Clear();
            ddlLinkType.Items.Add(new ListItem("-", ""));
            foreach (LinkType type in types)
            {
                ListItem li = new ListItem();
                li.Value = type.ID.ToString();
                li.Text = type.Title;
                ddlLinkType.Items.Add(li);
            }
        }


        protected void btnUp_Click(object sender, EventArgs e)
        {
            if (this.fuInsertLogo.PostedFile.FileName != "")
            {
                lblILogo.Text = FileUploads(this, fuInsertLogo, "..\\..\\UpLoad\\linkImage\\");
                lblIMessage.Text = "图片上传成功！";
                this.imgInsertLogo.ImageUrl = "../../UpLoad/linkImage/" + lblILogo.Text;
            }
            else
            {
                lblIMessage.Text = "请选择要上传的图片！";
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (txtTitle.Text.Trim() != "")
            {
                LinkInfo team = new LinkInfo();
                team.TypeID = Convert.ToInt32(ddlLinkType.SelectedValue);
                team.Title = txtTitle.Text.Trim();
                team.Location = txtLocation.Text.Trim();
                team.Image = lblILogo.Text;
                LinkInfoManager.AddLinkInfo(team);
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('添加友情链接信息成功。');window.opener.location.replace('Link_List.aspx');window.close();", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "aaa", "alert('请输入友情链接的标题。');", true);
            }

        }


        #region FileUp
        public static string FileUploads(Page page, FileUpload fileProduct, string url)
        {
            if (!fileProduct.HasFile)
            {
                return "出错";

            }
            try
            {
                string urlimg = DateTime.Now.ToString("hhmmss") + fileProduct.FileName;
                fileProduct.SaveAs(page.Server.MapPath(url) + urlimg);
                return urlimg;
            }
            catch
            {
                return "出错";
            }
        }
        #endregion
    }
}
